home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Mania 6
/
MacMania 6.toast
/
/
Tools&Utilities
/
EnterAct Stuff
/
write your own Drag_on
/
CodeResource_Files.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-12-22
|
3KB
|
133 lines
/* CodeResource_Files.c - read stdin, write stdout
using Mac calls rather than stdio. */
/* stdin and stdout are just ordinary text files, the full path
names for these files being supplied by the calling application
in gacc.stdInFileName and gacc.stdOutFileName - gacc is a global
copy of the AppCodeComm struct passed to the code resource by the
application (see Code_Main.c). The full path name is converted to
a regular file name and vRefNum, and then things proceed in a very
standard way, like reading or saving using SFGetFile or SFPutFile.
*/
#include "CodeResource.h"
#include "AppCodeComm.h"
#include <string.h>
Handle ReadStdIn(void);
Boolean PutToStdOut(Handle hText);
short GetNameAndVRefNum(char *name, char *filename);
/* Read in the contents of stdin to a handle. Note if there is
nothing in the file then NULL is returned rather than an empty
handle. The full path name of stdin is in gacc.stdInFileName. */
Handle ReadStdIn()
{
Handle hText = NULL;
long count;
short saveVol, refNum, vRefNum;
char filename[32];
Boolean opened = FALSE;
if (GetVol(NULL, &saveVol))
saveVol = 0;
if ((vRefNum = GetNameAndVRefNum(gacc.stdInFileName, filename)) == 0)
goto JumpOut;
if (FSOpen((StringPtr)filename, vRefNum, &refNum))
goto JumpOut;
else
opened = TRUE;
if (GetEOF(refNum, &count) || count <= 0L)
goto JumpOut;
hText = NewHandle(count);
if (MemError() != noErr)
{
MemoryAlert();
goto JumpOut;
}
if (FSRead(refNum, &count, *hText))
{
DisposHandle(hText);
hText = NULL;
goto JumpOut;
}
JumpOut:
if (opened)
FSClose(refNum);
if (saveVol)
SetVol(NULL, saveVol);
return(hText);
}
/* Write the text in hText to stdout as specified by the full path name
in gacc.stdOutFileName. */
Boolean PutToStdOut(Handle hText)
{
long count;
short saveVol, refNum, vRefNum;
char filename[32];
if (GetVol(NULL, &saveVol))
saveVol = 0;
if ((vRefNum = GetNameAndVRefNum(gacc.stdOutFileName, filename)) == 0)
goto JumpOut;
FSDelete((StringPtr)filename, vRefNum);
if (Create((StringPtr)filename, vRefNum, '????', 'TEXT'))
goto JumpOut;
if (FSOpen((StringPtr)filename, vRefNum, &refNum))
goto JumpOut;
count = GetHandleSize(hText);
if (FSWrite(refNum, &count, *hText))
{
FSClose(refNum);
goto JumpOut;
}
FSClose(refNum);
FlushVol(NULL, vRefNum);
if (saveVol)
SetVol(NULL, saveVol);
return(TRUE);
JumpOut:
if (saveVol)
SetVol(NULL, saveVol);
return(FALSE);
}
/* Convert the full path name "name" into filename and vRefNum.
"name" looks like "DiskName:folder1:folder2:...folderN:filename",
so we back up from the end of "name" to the first colon to determine
the filename. */
short GetNameAndVRefNum(char *name, char *filename)
{
WDPBRec theParms;
short len = strlen(name);
Ptr endPtr = name + len, startPtr = endPtr;
char volname[256];
while (startPtr >= name)
{
if (*startPtr == ':')
break;
--startPtr;
}
++startPtr;
if (startPtr >= endPtr)
return(0);
filename[0] = endPtr - startPtr;
BlockMove(startPtr, filename+1, filename[0]);
volname[0] = startPtr - name;
BlockMove(name, volname+1, startPtr - name);
theParms.ioVRefNum = 0;
theParms.ioNamePtr = (StringPtr)volname;
theParms.ioWDDirID = 0;
theParms.ioWDProcID = 'ERIK';
if (PBOpenWD(&theParms, FALSE)) /* IM IV pg 158 */
return(0);
return(theParms.ioVRefNum);
}